/*
 * Created on 2008-06-03
 * @author percy
 * @version 1.0
 * 
 */
package com.sitech.ibnms.util;

import java.util.*;
import java.text.*;

public class DateFormater
{
	public  final static String INVALIDDATE = "1950-01-01";
  public  final static String SQLTYPE = "ORACLE";

	public final static String getFormatTime(String formatType) {
		SimpleDateFormat formatter= new  SimpleDateFormat (formatType);
		return  formatter.format(new java.util.Date());
	}
	
	public final static String getFormatTime(String formatType,java.util.Date formatDate) {
		if( null==formatDate ){
			return "";
		}
		SimpleDateFormat formatter= new  SimpleDateFormat (formatType);
		return  formatter.format(formatDate);
	}
	
  //�ж������ַ�(yyyymmdd)�Ƿ���ȷ(���Ǻ��Ͻ�û�ж����ڵ�30��31��28��29�����ж�)
	public final static boolean isRightDate(String date_s) {
		boolean isRight=true;
		if(date_s.length()!=8){
			isRight=false;
		}else{
			String year_s=date_s.substring(0,4);
			String month_s=date_s.substring(4,6);
			String date_ss=date_s.substring(6,8);
			try{
				 int year_i=Integer.parseInt(year_s);
				 int month_i=Integer.parseInt(month_s);
				 int date_i=Integer.parseInt(date_ss);
				 if(year_i<1000){
				 	 isRight=false;
				 }
				 if(month_i<1 || month_i>12){
				 	 isRight=false;
				 }
				 if(date_i<1 || date_i>31){
				 	 isRight=false;
				 }
			}catch(Exception e){
				  isRight=false;
          e.printStackTrace();
      }
		}
		return  isRight;
	}
	
	public final static String getNowTime() {
		SimpleDateFormat formatter1
			= new  SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		return  formatter1.format(new Date());
	}

	public final static String getToday(){
		SimpleDateFormat formatter1
		= new  SimpleDateFormat ("yyyy-MM-dd");
		return  formatter1.format(new Date());
	}

/*
	���ʱ��ΪĬ��ֵINVALIDDATE,����ǰ̨��ʾ""
*/
	public final static String dateToString(Date dDate)throws ParseException{
		if( null==dDate ){
			return "";
		}
		
                SimpleDateFormat formatter1
                = new  SimpleDateFormat ("yyyy-MM-dd");
                if(formatter1.format(dDate).equals(INVALIDDATE)){
                	return "";
		}
                return  formatter1.format(dDate);
	}
	
/*
	���ʱ��ΪĬ��ֵINVALIDDATE,����ǰ̨��ʾ""
*/	public final static String datetimeToString(Date dDate)throws ParseException{
		if( null==dDate ){
			return "";
		}
		if(dateToString(dDate).equals("")){
			return "";
		}
                SimpleDateFormat formatter1
                = new  SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
                return  formatter1.format(dDate);
	}

	public final static Date stringToDate(String cDate,String cFormat) throws ParseException{
              	SimpleDateFormat formatter1 = new  SimpleDateFormat (cFormat);
               	return  formatter1.parse(cDate);
	}

	public final static Date stringToDate(String cDate) throws ParseException{
                try{
                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd");
                	return  formatter1.parse(cDate);
                }
                catch(ParseException e){
	                try{
	                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	                	return  formatter1.parse(cDate);
	                }
	                catch(ParseException ee){
	                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd HH:mm");
	                	return  formatter1.parse(cDate);
					        }
                }
	}

	public final static Date stringToDateTime(String cDate) throws ParseException{
                try{
                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
                	return  formatter1.parse(cDate);
                }
                catch(ParseException e){
	                try{
	                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd HH:mm");
	                	return  formatter1.parse(cDate);
	                }
	                catch(ParseException ee){
	                	SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd");
	                	return  formatter1.parse(cDate);
					        }      
               }
	}

	private final static String toOracleDate(Date dDate) throws ParseException{
		SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd");
		return  " to_date('" + formatter1.format(dDate) + "','yyyy-mm-dd') ";
	}

	public final static String toOracleDateTime(Date dDate) throws ParseException{
		SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		return  " to_date('" + formatter1.format(dDate) + "','yyyy-MM-dd HH24:mi:ss') ";
	}


	private final static String toSybaseDate(Date dDate) throws ParseException{
		SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd");
		return  " '" + formatter1.format(dDate) + "' ";
	}

	private final static String toSybaseDateTime(Date dDate) throws ParseException{
		SimpleDateFormat formatter1 = new  SimpleDateFormat ("yyyy-MM-dd");
		return  " '" + formatter1.format(dDate) + "' ";
	}

	public final static String toDBDate(Date dDate) throws ParseException{
		if(null==dDate){
			dDate = stringToDate(INVALIDDATE);
		}
		String cSQLDate = null;
		if(SQLTYPE.equals("ORACLE"))
			cSQLDate = toOracleDate(dDate);
		if(SQLTYPE.equals("SYBASE"))
			cSQLDate = toSybaseDate(dDate);
			return cSQLDate;
	}

	public final static String toDBDate(String cDate) throws ParseException{
		if(null==cDate){
			cDate = INVALIDDATE;
		}
		return toDBDate(stringToDate(cDate));
	}


	public final static String toDBDateTime(Date dDate) throws ParseException{
		if(null==dDate){
			dDate = stringToDate(INVALIDDATE);
		}
		String cSQLDate = null;
		if(SQLTYPE.equals("ORACLE"))
			cSQLDate = toOracleDateTime(dDate);
		if(SQLTYPE.equals("SYBASE"))
			cSQLDate = toSybaseDateTime(dDate);
			return cSQLDate;
	}

	public final static String toDBDateTime(String cDate) throws ParseException{
		if(null==cDate){
			cDate = INVALIDDATE;
		}
		return toDBDateTime(stringToDateTime(cDate));
	}

/**
 * (1) parameter description
 * @param date
 * @param type
 * @param timeInterval
 * @return
 * (2)function description
 * realize the time plus and minus
 * (3) loop condition
 *y: year
 * m: month
 * d: date
 * h:hour
 * f: minute(fenzhong)
 * s: second
 */
    public static Date computeDate(Date date, char type, int timeInterval){
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int Time_year = cal.get(Calendar.YEAR);
        int Time_month = cal.get(Calendar.MONTH);
        int Time_day = cal.get(Calendar.DAY_OF_MONTH);
        int   Time_hour = cal.get(Calendar.HOUR_OF_DAY);
        int  Time_minute =cal.get(Calendar.MINUTE);
        int Time_second = cal.get(Calendar.SECOND);

        switch(type){
            case'y':
                {
                    Time_year = Time_year + timeInterval;
                    cal.set(Calendar.YEAR,Time_year);
                }
                break;

            case'm':
                {
                    Time_month = Time_month + timeInterval;
                    cal.set(Calendar.MONTH,Time_month);
                }
                break;

            case'd':
                {
                    Time_day = Time_day + timeInterval;
                    cal.set(Calendar.DAY_OF_MONTH,Time_day);
                }
                break;

            case'h':
               {
                    Time_hour = Time_hour + timeInterval;
                    cal.set(Calendar.HOUR_OF_DAY,Time_hour);
                }
                break;

            case'f':
                {
                    Time_minute = Time_minute + timeInterval;
                    cal.set(Calendar.MINUTE,Time_minute);
                }
                break;

            case's':
                {
                    Time_second = Time_second + timeInterval;
                    cal.set(Calendar.SECOND,Time_second);
                }
                break;
            default:
                break;
        }
                date =    cal.getTime();
                return  date;
    }
  public static Date computeDate(String cDate, char type, int timeInterval){
		java.util.Date dDate = null;
		try{
			dDate = stringToDateTime(cDate);
		}
		catch(ParseException e){
			dDate = new Date();
		}
		return computeDate(dDate, type, timeInterval);
	}
    
	public final static String getHISTableName(Date dDate) throws ParseException {
		if (null == dDate) {
			dDate = new Date();
		}
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
		return formatter1.format(dDate);
	}
	
	/**
	 * ��ݴ����ʱ��������Ĳ�ֵ����ȡ������ʱ��
	 * �㷨��ͨ��ת���ɱ�׼�������ת����
	 * add by qiuzq 20080611.
	 * @param dDate �����ʱ��
	 * @param calValue �����Ĳ�ֵ
	 * @return �����ַ�
	 * @throws ParseException
	 */
	public final static String getCalDateStr(Date dDate,int calValue)throws ParseException{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long myTime = dDate.getTime() + calValue;
		dDate.setTime(myTime);
        return bartDateFormat.format(dDate);
	}
	
	/**
	 * ��ݴ����ʱ�䡢���ͺ������Ĳ�ֵ����ȡ������ʱ��
	 * �㷨��ͨ��ת���ɱ�׼�������ת����
	 * @param sDate
	 * @param type ���ͣ�������ȵ��죻Ŀǰ��֧������
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */	 
	public final static String getCalDateStr(String sDate,String type,int calValue)throws ParseException{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int iValue=0;
		if("day".equals(type.toLowerCase())){
			iValue=24*60*60*1000*calValue;
		}else if("hour".equals(type.toLowerCase())){
			iValue=60*60*1000*calValue;
		}else if("minute".equals(type.toLowerCase())){
			iValue=60*1000*calValue;
		}else if("second".equals(type.toLowerCase())){
			iValue=1000*calValue;
		}		
		return getCalDateStr(bartDateFormat.parse(sDate),iValue);
	}
	
	/**
	 * ��ݴ����ʱ�䡢���ͺ������Ĳ�ֵ����ȡ������ʱ��
	 * �㷨��ͨ��ת���ɱ�׼�������ת����
	 * @param dDate
	 * @param type ���ͣ�������ȵ��죻Ŀǰ��֧������
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */	 
	public final static String getCalDateStr(Date dDate,String type,int calValue)throws ParseException{
		int iValue=0;
		if("day".equals(type.toLowerCase())){
			iValue=24*60*60*1000*calValue;
		}else if("hour".equals(type.toLowerCase())){
			iValue=60*60*1000*calValue;
		}else if("minute".equals(type.toLowerCase())){
			iValue=60*1000*calValue;
		}else if("second".equals(type.toLowerCase())){
			iValue=1000*calValue;
		}		
		return getCalDateStr(dDate,iValue);
	}
	
	/**
	 * ��ݴ����ʱ�䡢���ͺ������Ĳ�ֵ����ȡ������ʱ��
	 * �㷨��ͨ��ת���ɱ�׼�������ת����
	 * @param dDate
	 * @param type ���ͣ�������ȵ��죻Ŀǰ��֧������
	 * @param calValue
	 * @return
	 * @throws ParseException
	 */	 
	public final static Date getCalDateTime(Date dDate,String type,int calValue)throws ParseException{
		int iValue=0;
		if("day".equals(type.toLowerCase())){
			iValue=24*60*60*1000*calValue;
		}else if("hour".equals(type.toLowerCase())){
			iValue=60*60*1000*calValue;
		}else if("minute".equals(type.toLowerCase())){
			iValue=60*1000*calValue;
		}else if("second".equals(type.toLowerCase())){
			iValue=1000*calValue;
		}		
		
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long myTime = dDate.getTime() + iValue;
		dDate.setTime(myTime);
		return dDate;
	}
	
	

	public static void main(String[] args){
//		String st = DateFormater.getFormatTime("yyyyMMddhh24missSSS");
		try{
			String st = DateFormater.getFormatTime("yyyyMMddHHmmssSSS");
			System.out.println(st);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
